package com.example.security.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @application:
 * @author: LiYuhang
 * @Date: 2021/5/29 19:35
 * @version: 0.1
 * @Description :
 * @Modified By:
 */

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {




    private static final Logger log = LoggerFactory.getLogger(MyAuthenticationSuccessHandler.class);

    @Override
    public void onAuthenticationSuccess(HttpServletRequest req,HttpServletResponse res,Authentication authentication) throws IOException, ServletException {
        res.setContentType("application/json;charset=utf-8");
        PrintWriter out = res.getWriter();
        Map<String, Object> map = new HashMap<>();
        map.put("status",200);
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        log.info("userDetails:{}",userDetails);
        map.put("msg","登陆成功");
        out.write(new ObjectMapper().writeValueAsString(map));
        out.flush();
        out.close();
    }


}
